package com.jastar.android.game.richman.logic.bean;

import com.jastar.android.game.richman.GameActivity;
import com.jastar.android.game.richman.logic.enums.CellType;
import com.jastar.android.game.richman.logic.enums.LandType;
import com.jastar.android.util.RandomUtil;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Cell {

    public static final int DEFAULT_PRICE = 1;
    public static final int DEFAULT_LEVEL = 1;
    private static List<Cell> instances;

    private final CellGroup group;
    private final CellType type;
    private final int position;
    private final int price;

    private Player owner = null;
    private LandType landType;
    private int level = DEFAULT_LEVEL;
    private int maxLevel;

    public static void randomLevelup(int num, Player p) {
        List<Cell> list = instances.stream()
                .filter(c -> null != c.getOwner() && c.getMaxLevel() > c.getLevel() && (null == p || c.getOwner()
                        .equals(p))).collect(Collectors.toList());
        if (list.size() <= num) {
            list.forEach(Cell::levelup);
        } else {
            int[] indexs = RandomUtil.randomNumber(0, list.size() - 1, num);
            Arrays.stream(indexs).forEach(i -> list.get(i).levelup());
        }
    }

    public static int randomDestroy(int num) {
        int res;
        List<Cell> list = instances.stream().filter(c -> null != c.getOwner()).collect(Collectors.toList());
        if (list.size() <= num) {
            list.forEach(Cell::clean);
            res = list.size();
        } else {
            int[] indexs = RandomUtil.randomNumber(0, list.size() - 1, num);
            Arrays.stream(indexs).forEach(i -> list.get(i).clean());
            res = num;
        }
        return res;
    }

    public void clean() {
        this.setLevel(Cell.DEFAULT_LEVEL);
        this.setOwner(null);
    }

    public static void initInstances(List<Cell> list) {
        instances = list;
    }

    public Cell(CellGroup group, CellType type, int position, int price) {
        this.group = group;
        this.type = type;
        this.position = position;
        this.price = price;
    }

    public void hand(GameActivity activity, Player player, CellType.Callback callback) {
        getType().getHandler().hand(activity, this, player, callback);
    }

    public boolean isMaxLevel() {
        return maxLevel == level;
    }

    public boolean levelup() {
        if (maxLevel <= level) return false;
        level += 1;
        return true;
    }

    /** 过路费 */
    public int getToll() {
        switch (landType) {
            case CHAIN:
                return instances.stream()
                        .filter(c -> this.getLandType() == c.getLandType() && this.getOwner() == c.getOwner() && this.getGroup()
                                .equals(c.getGroup()) && this.getLevel() == c.getLevel())
                        .mapToInt(c -> c.getLevel() * c.getPrice()).sum();
            case HOUSE:
            default:
                return level * price;
        }
    }

    public int getColor() {
        return (owner != null) ? owner.getColor() : type.getColor();
    }

    public CellGroup getGroup() {
        return group;
    }

    public CellType getType() {
        return type;
    }

    public int getPosition() {
        return position;
    }

    public int getPrice() {
        return price;
    }

    public void setMaxLevel(int maxLevel) {
        this.maxLevel = maxLevel;
    }

    public int getMaxLevel() {
        return maxLevel;
    }

    public Player getOwner() {
        return owner;
    }

    public void setOwner(Player owner) {
        this.owner = owner;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public void setLandType(LandType landType) {
        this.landType = landType;
    }

    public LandType getLandType() {
        return landType;
    }

}